﻿class BooktablesController < ApplicationController
  # GET /booktables
  # GET /booktables.json
  def index
    if(session[:user_id])
      @user = User.find(session[:user_id])
    else
      @user = -1
    end

    if(params[:search]!=nil)
      @booktables = Booktable.search(params[:search])
    end

    if(params[:searchByAuthor]!=nil)
      @booktables = Booktable.searchByAuthor(params[:searchByAuthor])
    end

    if(params[:searchByCategory]!=nil)
      @booktables = Booktable.searchByCategory(params[:searchByCategory])
    end
    
    if(params[:search]==nil && params[:searchByCategory]==nil && params[:searchByAuthor]==nil)
      @booktables = Booktable.all
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @booktables }
    end
  end

  # GET /booktables/1
  # GET /booktables/1.json
  def show
    if(session[:user_id])
      @user = User.find(session[:user_id])
    else
      @user = -1
    end
    @booktable = Booktable.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @booktable }
    end
  end

  # GET /booktables/new
  # GET /booktables/new.json
  def new
    @booktable = Booktable.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @booktable }
    end
  end

  # GET /booktables/1/edit
  def edit
    @booktable = Booktable.find(params[:id])
  end

  # POST /booktables
  # POST /booktables.json
  def create
    @booktable = Booktable.new(params[:booktable])

    respond_to do |format|
      if @booktable.save
        format.html { redirect_to @booktable, notice: "#{t(:addsus)}" }
        format.json { render json: @booktable, status: :created, location: @booktable }
      else
        format.html { render action: "new" }
        format.json { render json: @booktable.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /booktables/1
  # PUT /booktables/1.json
  def update
    @booktable = Booktable.find(params[:id])

    respond_to do |format|
      if @booktable.update_attributes(params[:booktable])
        format.html { redirect_to @booktable, notice: "#{t(:statusupdate)}" }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @booktable.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /booktables/1
  # DELETE /booktables/1.json
  def destroy
    @booktable = Booktable.find(params[:id])
    @booktable.destroy

    respond_to do |format|
      format.html { redirect_to booktables_url }
      format.json { head :no_content }
    end
  end
  def searchName
    
  end
  def searchCategory
    
  end
  def searchAuthor
    
  end
end
